package com.example.electricbicycle.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.example.electricbicycle.po.Customer;
import com.example.electricbicycle.po.User;
import com.example.electricbicycle.service.CustomerService;
import com.example.electricbicycle.service.CustomerServiceImpl;

@WebServlet("/UpdateCustomerServlet")
public class UpdateCustomerServlet extends HttpServlet {
    private final CustomerService customerService = new CustomerServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();

        // 清除所有旧消息
        session.removeAttribute("successMsg");
        session.removeAttribute("errorMsg");

        try {
            // 1. 获取当前登录用户
            User currentUser = (User) session.getAttribute("currentUser");

            if (currentUser == null) {
                session.setAttribute("errorMsg", "请先登录系统");
                response.sendRedirect(request.getContextPath() + "/login.jsp");
                return;
            }

            // 2. 获取要编辑的客户ID
            String uidParam = request.getParameter("id");
            if (uidParam == null || uidParam.trim().isEmpty()) {
                session.setAttribute("errorMsg", "无效的客户ID");
                response.sendRedirect(request.getContextPath() + "/CustomerListServlet");
                return;
            }

            int uid = Integer.parseInt(uidParam);

            // 3. 获取客户信息
            Customer customer = customerService.getCustomerById(uid);

            if (customer != null) {
                request.setAttribute("customer", customer);
                request.getRequestDispatcher("/admin/updatecustomer.jsp").forward(request, response);
            } else {
                session.setAttribute("errorMsg", "客户不存在");
                response.sendRedirect(request.getContextPath() + "/CustomerListServlet");
            }

        } catch (NumberFormatException e) {
            session.setAttribute("errorMsg", "无效的客户ID格式");
            response.sendRedirect(request.getContextPath() + "/CustomerListServlet");
        } catch (Exception e) {
            session.setAttribute("errorMsg", "系统错误: " + e.getMessage());
            response.sendRedirect(request.getContextPath() + "/CustomerListServlet");
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();

        // 清除所有旧消息
        session.removeAttribute("successMsg");
        session.removeAttribute("errorMsg");

        try {
            // 1. 获取当前登录用户
            User currentUser = (User) session.getAttribute("currentUser");

            if (currentUser == null) {
                session.setAttribute("errorMsg", "请先登录系统");
                response.sendRedirect(request.getContextPath() + "/login.jsp");
                return;
            }

            // 2. 获取客户ID
            int userId = Integer.parseInt(request.getParameter("id"));

            // 3. 创建客户对象
            Customer customer = new Customer();
            customer.setId(userId);
            customer.setName(request.getParameter("name"));
            customer.setPhone(request.getParameter("phone"));
            customer.setAddress(request.getParameter("address"));

            // 4. 更新客户
            int isUpdated = customerService.updateCustomer(customer);

            // 5. 处理结果
            if (isUpdated > 0) {
                session.setAttribute("successMsg", "客户信息更新成功");
            } else {
                session.setAttribute("errorMsg", "客户信息更新失败");
            }

        } catch (Exception e) {
            session.setAttribute("errorMsg", "系统错误: " + e.getMessage());
            e.printStackTrace();
        }

        response.sendRedirect(request.getContextPath() + "/CustomerListServlet");
    }
}